package com.qing.springboot01.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.springboot01.common.R;
import com.qing.springboot01.entity.Dish;
import com.qing.springboot01.entity.Employee;
import com.qing.springboot01.service.CategoryService;
import com.qing.springboot01.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.stream.Collectors;
import  com.qing.springboot01.dto.DishDto;
import  com.qing.springboot01.entity.Category;
import com.qing.springboot01.entity.DishFlavor;
import  com.qing.springboot01.service.DishFlavorService;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author tina
 * @since 2023-06-26 04:09:20
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService ;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishDto.setIsDeleted(0);
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功!");
    }

    /**
     * 菜品分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        // 构造分页构造器
        final Page<Dish> pageInfo = new Page<>(page, pageSize);
        final Page<DishDto> dishDtoPage = new Page<>();
        final LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(name!=null,Dish::getName,name);
        // 排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        // 执行分页查询
        dishService.page(pageInfo,queryWrapper);
        // 对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        List<Dish> records = pageInfo.getRecords();
        // 基于dish生产dishDto的list
        List<DishDto> list = records.stream().map((item)->{
            // 构造对象
            final DishDto dishDto = new DishDto();
            // 获取分类名
            final Long categoryId = item.getCategoryId();
            final Category category = categoryService.getById(categoryId);
            String categoryName ="";
            if (category!=null) {
                categoryName = category.getName();
            }
            // 为对象赋值
            BeanUtils.copyProperties(item,dishDto);
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());
        // 设置
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    /**
     * 编辑回显,根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        System.out.println(id);
        final DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info("修改菜品 {}",dishDto);
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功!");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除菜品 {}",ids);
        dishService.deleteWithFlavor(ids);
        return R.success("删除菜品成功");
    }

    /**
     * 批量修改状态方法
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@RequestParam List<Long> ids, @PathVariable Integer status){
        List<Dish> dishList= ids.stream().map((id)->{
            final Dish d = new Dish();
            d.setStatus(status);
            d.setId(id);
            return d;
        }).collect(Collectors.toList());
        dishService.updateBatchById(dishList);
        return R.success(status==0?"已禁售":"已启售");
    }

    /**
     * 根据分类查询菜品
     * @param dish
     * @return
     */
//    @GetMapping("/list")
//    public R<List<Dish>> list(Dish dish){
//        final LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
//        // 根据分类查询
//        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        // 仅查询起售状态的菜品
//        queryWrapper.eq(Dish::getStatus,1);
//        // 排序
//        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        final List<Dish> list = dishService.list(queryWrapper);
//        return R.success(list);
//    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
//        System.out.println(dish);
        log.info("---------------------",dish.getCategoryId());
        final Long categoryId = dish.getCategoryId();
        // 根据分类查询菜品
        final LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        // 筛选分类
        wrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        // 筛选 是否启售
        wrapper.eq(Dish::getStatus,1);
        // 排序
        wrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        final List<Dish> list = dishService.list(wrapper);
        List<DishDto> dishDtoList = list.stream().map((item)->{
            final DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            final LambdaQueryWrapper<DishFlavor> q = new LambdaQueryWrapper<>();
            q.eq(DishFlavor::getDishId,item.getId());
            final List<DishFlavor> dishFlavorList = dishFlavorService.list(q);
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtoList);
    }
}
